package cn.txm.user.controller;

import cn.txm.basic.util.AjaxResult;
import cn.txm.basic.util.FastDfsUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/fastdfs")
public class FastdfsController {
    /**
     * 文件上传
     */
    @RequestMapping("/upload")
    public AjaxResult upload(MultipartFile file){
        try {
            //获取上传的文件名称
            String filename = file.getOriginalFilename();
            //获取后缀名称
            String extname = filename.substring(filename.lastIndexOf(".") + 1);

            String filepath = FastDfsUtils.upload(file.getBytes(), extname);
            return AjaxResult.me().setData(filepath);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("上传失败"+e.getMessage());
        }
    }
}
